package org.flex.chat;

import java.io.IOException;
import java.util.Vector;

public class Boast {
	private static Boast instance;

	public static Boast getInstance() {
		if (instance == null)
			instance = new Boast();
		return instance;
	}

	private Vector<Room> v = new Vector<Room>();

	public Vector<Room> getV() {
		return v;
	}

	public void removeAllRoom() {
		v.clear();
	}

	/**
	 * ���ҷ���
	 * 
	 * @param code
	 * @return
	 */
	private Room findRoomByCode(String code) {
		for (Room r : v) {
			if (r.getCode().equalsIgnoreCase(code))
				return r;
		}
		return null;
	}

	/**
	 * ��������
	 * 
	 * @param code
	 * @param name
	 */
	public void addRoom(String code, String name) {
		v.add(new Room(code, name));
	}

	public void removeRoom(String code) {
		Room r = findRoomByCode(code);
		if (r == null)
			return;
		synchronized (v) {
			v.remove(r);
		}
	}

	public void closeAllConnection() {
		for (Room r : v) {
			for (RequestProccess rp : r.getV()) {
				rp.setCancle(false);
			}
		}
	}

	public void viewRooms() {
		for (Room r : v) {
			System.out.println(r.getName() + ":(" + r.getCode() + ") ��ǰ����"
					+ r.getV().size() + "人");
		}

	}

	public void kick(String room, String user) {
		for (Room r : v) {
			if (r.getCode().equals(room)) {
				RequestProccess rp1 = null;
				searchUser: for (RequestProccess rp : r.getV()) {
					if (rp.getUserName().equals(user)) {
						rp1 = rp;
						break searchUser;
					}
				}
				if (rp1 != null) {
					rp1.setCancle(true);
					r.getV().remove(rp1);
				}
			}
		}
	}

	/**
	 * ����
	 * 
	 * @param rp
	 * @param room
	 */
	public void add(RequestProccess rp, String room) {
		Room r = findRoomByCode(room);
		if (r != null)
			r.add(rp);
	}

	/**
	 * ȡ�÷���
	 * 
	 * @return
	 */
	public String getRooms() {
		String ret = "<root>";
		for (Room r : v)
			ret += "<room name=\"" + r.getName() + "\" code=\"" + r.getCode()
					+ "\"/>";
		ret += "</root>";
		return ret;

	}

	/**
	 * ��ѯ������Ա
	 * 
	 * @param room
	 */
	public void sendOnlineUsers(String room) {
		Room r = findRoomByCode(room);
		if (r == null)
			return;
		// ���Ͳ������

		String ret = "<root><onlineusers>";
		for (RequestProccess rp : r.getV()) {
			if (!rp.isConnected())
				continue;
			ret += "<user name=\"" + rp.getUserName() + "\" face=\""
					+ rp.getFace() + "\" vt=\""
					+ (rp.getVideoToke() == null ? "" : rp.getVideoToke())
					+ "\"></user>";
		}
		ret += "</onlineusers></root>";
		boastMessage(ret, room);
	}

	/**
	 * ��ѯ������Ա
	 * 
	 * @param room
	 */
	public void sendOnlineUsers(String room, String mes) {
		Room r = findRoomByCode(room);
		if (r == null)
			return;
		// ���Ͳ������

		String ret = "<root><onlineusers>";
		for (RequestProccess rp : r.getV()) {
			if (!rp.isConnected())
				continue;
			ret += "<user name=\"" + rp.getUserName() + "\" face=\""
					+ rp.getFace() + "\" vt=\""
					+ (rp.getVideoToke() == null ? "" : rp.getVideoToke())
					+ "\"></user>";
		}
		ret += "</onlineusers>" + mes + "</root>";
		boastMessage(ret, room);
	}

	/**
	 * ����˽����Ϣ
	 * 
	 * @param user
	 * @param mes
	 */
	public void sendSingleUserMessage(String room, String user, String mes) {
		Room r = findRoomByCode(room);
		if (r == null)
			return;
		for (RequestProccess rp : r.getV()) {
			if (rp.getUserName().equalsIgnoreCase(user)) {
				try {
					rp.sendMessage(mes);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return;
			}
		}
	}

	/**
	 * �㲥Ⱥ����Ϣ
	 * 
	 * @param mes
	 * @param room
	 */
	public void boastMessage(String mes, String room) {
		Room r = findRoomByCode(room);
		if (r == null)
			return;
		if (r.boastMessage(mes) != null) {
			sendOnlineUsers(room);
		}
	}
}
